# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=ruby-protocol-http1
pkgver=0.15.0
pkgrel=1
pkgdesc='A low level implementation of the HTTP/1 protocol'
arch=(any)
url='https://github.com/socketry/protocol-http1'
license=(MIT)
depends=(ruby-protocol-http)
checkdepends=(ruby-bake-modernize ruby-bake-test ruby-bake-test-external ruby-async-io ruby-covered
              ruby-sus)
options=(!emptydirs)
source=(https://github.com/socketry/protocol-http1/archive/v$pkgver/$pkgname-$pkgver.tar.gz
        $pkgname-test-dependencies.patch::https://github.com/socketry/protocol-http1/pull/15.patch)
sha256sums=('cdb044d2fb8daa9bd85f65fb209aec09e032804cf211d7d2257c1d423e152751'
            '01aff72318d4ab71c699585b8ddc814587f4d9ed0834dc10ca470dfc989e5702')

prepare() {
  cd protocol-http1-$pkgver
  patch -p1 -i ../$pkgname-test-dependencies.patch
  sed -r -e 's|~>|>=|g' -e '/signing_key/d' -i protocol-http1.gemspec
  sed -i '/bake-gem/d;/bake-github-pages/d;/utopia-project/d;s/gem "stringio".*$/gem "stringio"/' gems.rb
}

build() {
  local _gemdir="$(gem env gemdir)"
  cd protocol-http1-$pkgver
  gem build protocol-http1.gemspec
  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/$_gemdir" \
    --bindir "tmp_install/usr/bin" \
    protocol-http1-$pkgver.gem
  find "tmp_install/$_gemdir/gems/" \
    -type f \
    \( \
        -iname "*.o" -o \
        -iname "*.c" -o \
        -iname "*.so" -o \
        -iname "*.time" -o \
        -iname "gem.build_complete" -o \
        -iname "Makefile" \
    \) \
    -delete
  rm -r tmp_install/$_gemdir/cache
}

check() {
  local _gemdir="$(gem env gemdir)"
  cd protocol-http1-$pkgver
  GEM_HOME="tmp_install/$_gemdir" bake test || echo "Expected test failure because our stringio is too old"
}

package() {
  cd protocol-http1-$pkgver
  cp -a tmp_install/* "$pkgdir"/
  install -Dm644 license.md -t "$pkgdir"/usr/share/licenses/$pkgname/
}
